import { defineStore } from 'pinia'
import { getUserInfoApi } from '@/api/userinfo.js'
import { ref } from 'vue'
export const useUserInfoStore = defineStore(
  'userinfo',
  () => {
    const userInfo = ref({
      account: '',
      name: '',
      sex: '',
      department: '',
      identity: '',
      email: '',
      image_url: '',
      id: '',
      status: '',
    })

    // 获取用户信息
    const getUserInfo = async (id) => {
      const res = await getUserInfoApi(id)
      userInfo.value.account = res.data.result.account
      userInfo.value.name = res.data.result.name
      userInfo.value.sex = res.data.result.sex
      userInfo.value.department = res.data.result.department
      userInfo.value.identity = res.data.result.identity
      userInfo.value.email = res.data.result.email
      userInfo.value.image_url = res.data.result.image_url
      userInfo.value.id = res.data.result.id
      userInfo.value.status = res.data.result.status
    }
    // 更改 image_url
    const changeImageUrl = (url) => {
      userInfo.value.image_url = url
    }
    // 更改id
    const changeId = (id) => {
      userInfo.value.id = id
    }
    return { userInfo, getUserInfo, changeImageUrl, changeId }
  },
  {
    persist: true,
  }
)
